<?php
namespace DuxCms\Service;
/**
 * 标签接口
 */
class LabelService{
	/**
	 * 栏目列表
	 */
	public function categoryList($data){
        $where='';
        //上级栏目
        if(isset($data['parent_id'])){
            $where['parent_id'] = $data['parent_id'];
        }
        //指定栏目
        if(!empty($data['class_id'])){
            $where['class_id'] = array('in',$data['class_id']);
        }
        //栏目属性
        if(isset($data['type'])){
            if($data['type']){
                $where['B.type'] = 1;
            }else{
                $where['B.type'] = 0;
            }
        }
        //其他条件
        if(!empty($data['where'])){
            $where['_string'] = $data['where'];
        }
        //排序
        if(!empty($data['order'])){
            $order=$data['order'];
        }
        //其他属性
        $where['show'] = 1;
        $model = D('DuxCms/Category');
        $list=$model->loadData($where,$data['limit'],$order);
        if(empty($list)){
            return array();
        }
        $data=array();
        $i = 0;
        foreach ($list as $key => $value) {
            $data[$key]=$value;
            $data[$key]['i']=$i++;
            $data[$key]['curl']=$model->getUrl($value);
        }
        return $data;
	}

    /**
     * 内容列表
     */
    public function contentList($data){
        $where=array();
        //指定栏目内容
        if(!empty($data['class_id'])){
            $where['A.class_id'] = array('in',$data['class_id']);
        }
        //指定栏目下子栏目内容
        if ($data['type']=='sub'&&!empty($data['class_id'])) {
            $classIds = D('DuxCms/Category')->getSubClassId($data['class_id']);
            $where['A.class_id'] = array('in',$classIds);
        }
        //是否带形象图
        if (isset($data['image'])) {
            if($data['image'] == true)
            {
                $where['A.image'] = array('neq','');
            }else{
                $where['A.image'] = '';
            }
        }
        //调用推荐位
        if(!empty($data['pos_id'])){
            $where['_string'] = 'find_in_set('.$data['pos_id'].',A.position) ';
        }
        //其他条件
        if (!empty($data['where'])) {
            $where['_string'] = $data['where'];
        }
        //调用数量
        if (empty($data['limit'])) {
            $data['limit'] = 10;
        }
        //内容排序
        if(empty($data['order'])){
            $data['order']='A.sequence ASC,A.time DESC,A.content_id DESC';
        }
        //其他属性
        $where['status'] = 1;
        $model = D('DuxCms/Content');
        $list=D('DuxCms/Content')->loadList($where,$data['limit'],$data['order']);
        if(empty($list)){
            return array();
        }
        $data=array();
        $i = 0;
        $modelCategory = D('DuxCms/Category');
        foreach ($list as $key => $value) {
            $data[$key] = $value;
            $data[$key]['i'] = $i++;
            $data[$key]['curl'] = $modelCategory->getUrl($value);
            $data[$key]['aurl'] = $model->getUrl($value);
        }
        return $data;
    }

    /**
     * 碎片调用
     */
    public function fragment($data){
        $where=array();
        if(empty($data['mark'])){
            return ;
        }
        $where['label'] = $data['mark'];
        $info = D('DuxCms/Fragment')->getWhereInfo($where);
        if(empty($info)){
            return ;
        }
        return htmlspecialchars_decode($info['content']);
    }

    /**
     * 表单token
     */
    public function formToken($data){
        $where=array();
        if(empty($data['name'])){
            return ;
        }
        $where = array();
        $where['table'] = $data['name'];
        $formInfo = D('DuxCms/FieldsetForm')->getWhereInfo($where);
        if(empty($formInfo)){
            return ;
        }
        return D('DuxCms/FieldsetForm')->setToken($data['name']);
    }

    /**
     * 内容链接调用
     */
    public function aurl($data){
        if(empty($data['content_id'])){
            return ;
        }
        $where=array();
        $where['content_id'] = $data['content_id'];
        $info = D('DuxCms/Content')->getWhereInfo($where);
        if(empty($info)){
            return ;
        }
        return D('DuxCms/Content')->getUrl($info);
    }

    /**
     * 栏目链接调用
     */
    public function curl($data){
        if(empty($data['class_id'])){
            return ;
        }
        $where=array();
        $where['class_id'] = $data['class_id'];
        $info = D('DuxCms/Category')->getWhereInfo($where);
        if(empty($info)){
            return ;
        }
        return D('DuxCms/Category')->getUrl($info);
    }
}
